# -*- coding:utf-8 -*-

""".ini parser"""

import os
import hashlib
import configparser

class CnfIni(object):
    """create ini parser"""

    def __init__(self, path):
        self.__cfg = dict()
        try:
            if not os.path.isfile(path):
                raise IOError(path + ' exists')
            else:
                self.__cnf = configparser.ConfigParser()
                self.__cnf.read(path)
        except IOError, msg:
            print msg
            exit(1)

    def get_attr(self, group, name):
        """
        get ini options value
        Parameters:
            group - ini group
            name - options name
        Returns:
            string options value
        """
        md5 = hashlib.md5()
        md5.update(str(group).strip() + str(name).strip())
        gkey = md5.hexdigest()

        if gkey not in self.__cfg:
            try:
                val = self.__cnf.get(group, name)
            except configparser.NoOptionError:
                return ''
            except configparser.NoSectionError:
                return ''
            else:
                self.__cfg.update({gkey: val})

        return self.__cfg.get(gkey)
